package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/users")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    //1. 用户列表查询
    @RequestMapping
    public Result list(UserDto userDto) {
        log.info("列表查询: {}", userDto);
        PageResult<User> pageResult = userService.list(userDto);
        return Result.success(pageResult);
    }
    //3. 添加用户
    @Log
    @PostMapping
    public Result add(@RequestBody User user){
        log.info("添加用户：{}", user);
        userService.add(user);
        return Result.success();
    }
    //2. 删除用户
    @Log
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List< Integer> ids) {
        log.info("删除用户：{}", ids);
        userService.deleteByIds(ids);
        return Result.success();
    }
    //4. 根据ID查询
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询id：{}", id);
        User user = userService.getById(id);
        return Result.success(user);
    }
    //5. 修改用户
    @Log
    @PutMapping
    public Result update(@RequestBody User user) {
        log.info("修改用户：{}", user);
        userService.update(user);
        return Result.success();
    }
    //6. 查询所有用户
    @GetMapping("/list")
    public Result list() {
        log.info("查询所有用户");
        List<User> userList = userService.list();
        return Result.success(userList);
    }
    //7. 根据角色查询用户
    @GetMapping("/role/{roleLabel}")
    public Result listByRole(@PathVariable String roleLabel) {
        log.info("根据角色查询用户：{}", roleLabel);
        List<User> userList = userService.listByRole(roleLabel);
        return Result.success(userList);
    }
    //8. 根据部门查询用户
    @GetMapping("/dept/{deptId}")
    public Result listByDept(@PathVariable Integer deptId) {
        log.info("根据部门查询用户：{}", deptId);
        List<User> userList = userService.listByDept(deptId);
        return Result.success(userList);
    }
}
